package com.bookm.bookm.service.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanListHandler;
import com.bookm.bookm.entity.BookingVO;
import com.bookm.bookm.entity.Result;
import com.bookm.bookm.service.BookingService;
import com.bookm.bookm.utils.EntityUtil;
import com.bookm.bookm.utils.ResultUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//预定功能
public class BookingServiceImpl implements BookingService {
    @Override
    public Result getBookingList() {
        ArrayList<BookingVO> res = new ArrayList<BookingVO>();
        try {
            EntityUtil entityUtil = new EntityUtil();
            ConfigurationserviceImpl configurationservice = new ConfigurationserviceImpl();
            String bookingDay = configurationservice.getval("Estvaldays");
            int i = Integer.parseInt(bookingDay);
            List<BookingVO> result1 = Db.use().query("select book.book_id,book_name,reader.id,name,booking.rd_date,college,professional,num from book,booking,reader where book.book_id = booking.book_id and reader.id = booking.id and date_add(booking.rd_date, INTERVAL ? DAY )< now()", BeanListHandler.create(BookingVO.class),i);
            if(result1 != null){
                result1.forEach(x->{
                BookingServiceImpl bookingService = new BookingServiceImpl();
                Result y = bookingService.deleteBooking(x.getId(),x.getBookId());
                });
            }
            List<Entity> result = Db.use().query("select book.book_id,book_name,reader.id,name,booking.rd_date,college,professional,num from book,booking,reader where book.book_id = booking.book_id and reader.id = booking.id");
            for(Entity entity : result){
                BookingVO bookingVO = entityUtil.toObj(new BookingVO(),entity);
                res.add(bookingVO);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Result success = ResultUtil.success(res);
        return success;
    }

    @Override
    public Result getOneBooking(String searchText) {
        ArrayList<BookingVO> res = new ArrayList<BookingVO>();
        EntityUtil entityUtil = new EntityUtil();
        try {
            List<Entity> result = Db.use().query("select book.book_id,book_name,reader.id,name,booking.rd_date,college,professional,num from book,booking,reader where book.book_id = booking.book_id and reader.id = booking.id and (book_name like ? or reader.id like ?)",'%'+searchText+'%','%'+searchText+'%');
            for(Entity entity: result){
                BookingVO bookingVO = entityUtil.toObj(new BookingVO(),entity);
                res.add(bookingVO);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Result success = ResultUtil.success(res);
        return success;
    }

    @Override
    public Result deleteBooking(String id, String bookId) {
        int i=0;
        try {
            i = Db.use().del(Entity.create("booking").set("id",id).set("book_id",bookId));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(i == 1){
            return ResultUtil.success(i);
        }else {
            return ResultUtil.failure();
        }
    }

    @Override
    public Result addBooking(String id, String bookId) {
        int i=0;
        Date rdDate = new Date();
        try {
            i = Db.use().insert(Entity.create("booking").set("id",id).set("book_id",bookId).set("rd_date",rdDate));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(i == 1){
            return ResultUtil.success(i);
        }else {
            return ResultUtil.failure();
        }
    }

    @Override
    public Result getReaderBookingList(String id) {
        ArrayList<BookingVO> BookingVOList = new ArrayList<>();
        try {
            ConfigurationserviceImpl configurationservice = new ConfigurationserviceImpl();
            String bookingDay = configurationservice.getval("Estvaldays");
            int i = Integer.parseInt(bookingDay);
            List<BookingVO> result1 = Db.use().query("select book.book_id,book_name,reader.id,name,booking.rd_date,college,professional,num from book,booking,reader where book.book_id = booking.book_id and reader.id = booking.id and date_add(booking.rd_date, INTERVAL ? DAY )< now()", BeanListHandler.create(BookingVO.class),i);
            if(result1 != null){
                result1.forEach(x->{
                    BookingServiceImpl bookingService = new BookingServiceImpl();
                    Result y = bookingService.deleteBooking(x.getId(),x.getBookId());
                });
            }
            List<Entity> entityList = Db.use().query("select book.book_id,book_name,reader.id,name,book.rd_date,college,professional,num from book,booking,reader where book.book_id = booking.book_id and reader.id = booking.id and reader.id = ?",id);
            for(Entity entity:entityList){
                BookingVO bookingVO = EntityUtil.toObj(new BookingVO(),entity);
                BookingVOList.add(bookingVO);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResultUtil.success(BookingVOList);
    }
}
